home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / FLOWER_M / BEAR.C
C/C++ Source or Header  |  1990-03-20  |  4KB  |  190 lines

  1. /*
  2.     Flower Maker.c
  3.     Copyright 1990 Josh Pritikin
  4.     Public Domain!
  5.     
  6.     3.14.90 Finished first draft (.9)
  7.     3.18.90    Added user interface (1.0)
  8.     3.20.90 Cleaned up a few bugs (1.1)
  9. */
  10.  
  11. #include "stdio.h"
  12. #include "SANE.h"
  13.  
  14. double        fact[10];
  15. int            form[10], width, height;
  16.  
  17. #define onerad    (3.141592654/180)
  18. #define maxradius    120
  19.  
  20. main()
  21.     {
  22.     WindowPtr    window;
  23.     int            i, family, scrap, factused;
  24.     char        response[64];
  25.     
  26.     InitGraf(&thePort);
  27.     
  28.     printf("\n");
  29.     window = FrontWindow();
  30.     SetPort(window);
  31.     SetWTitle(window, "\pFlower Maker v1.0");
  32.     PenNormal();
  33.     SetOrigin(0,0);
  34.     
  35.     width = window->portRect.right-window->portRect.left;
  36.     height = window->portRect.bottom-window->portRect.top-15;
  37.     
  38.     family = 0;
  39.     
  40.     printf("Flower Maker v1.1\nCopyright 1990 Josh Pritikin\nPublic Domain\n\n");
  41.     do    {
  42.         printf("\nWhich equation template do you want to use?\n");
  43.         printf("1. sin(x)\n");
  44.         printf("2. cos(x)\n");
  45.         printf("3. sin(x)+cos(x)\n");
  46.         printf("4. sin(x)+sin(x)\n");
  47.         printf("5. sin(x)+sin(x)+cos(x)\n");
  48.         printf("6. sin(x)+sin(x)+sin(x)\n");
  49.         printf("7. sin(x)+sin(x)+sin(x)+sin(x)\n");
  50.         printf("r. random\n");
  51.         printf("q. quit\n");
  52.         do {
  53.             if (family) printf("(%i):", family);
  54.             else printf("(r):");
  55.             i= 0;
  56.             do {
  57.                 scanf("%c", &response[i]);
  58.             } while (response[i++] != '\n');
  59.             if (response[0] == 'q') goto exit;
  60.         } while (nbtwn(response[0], '1', '7') && response[0] != 'r' &&
  61.             response[0] != '\n');
  62.         printf("\n");
  63.         
  64.         if (btwn(response[0], '1', '7')) family = response[0] - '1' +1;
  65.         else if (response[0] == 'r') family = 0;
  66.         
  67.         for (i=0; i < TickCount()%100; i++)        /* pseudo-randomize */
  68.             scrap = Random();
  69.  
  70.         switch(family)
  71.             {
  72.             case 0:    printf("random\n"); break;
  73.             case 1:    printf("r = sin(x1*x)\n"); form[0] = 0; break;
  74.             case 2:    printf("r = cos(x1*x)\n"); form[0] = 1; break;
  75.             case 3:    printf("r = sin(x1*x)+cos(x2*x)\n"); form[0] = 0; form[1] = 1; break;
  76.             case 4:    printf("r = sin(x1*x)+sin(x2*x)\n"); form[0] = 0; form[1] = 0; break;
  77.             case 5:    printf("r = sin(x1*x)+sin(x2*x)+cos(x3*x)\n");
  78.                     form[0] = form[1] = 0;
  79.                     form[2] = 1;
  80.                     break;
  81.             case 6:    printf("r = sin(x1*x)+sin(x2*x)+sin(x3*x)\n");
  82.                     form[0] = form[1] = form[2] = 0;
  83.                     break;
  84.             case 7:    printf("r = sin(x1*x)+sin(x2*x)+sin(x3*x)+sin(x4*x)\n");
  85.                     form[0] = form[1] = form[2] = 0;
  86.                     break;
  87.             }
  88.         
  89.         if (family)
  90.             {
  91.             factused = (family-1)/2+1;
  92.             for (i=0; i < factused; i++)
  93.                 {
  94.                 printf("enter x%i (%g):", i+1, fact[i]);
  95.                 scanf("%lf", &fact[i]);
  96.                 scanf("%c", response);    /* dunno why this is nessesary */
  97.                 }
  98.             }
  99.         else
  100.             {
  101.             factused = Rand(1, 4);
  102.             for (i=0; i <= factused; i++)
  103.                 {
  104.                 fact[i] = (double)Rand(1, 10000)/1000;
  105.                 form[i] = 0;
  106.                 }
  107.             }
  108.         
  109.         EraseRect(&window->portRect);
  110.         printf("\n\nr = %g*(", (double)maxradius/factused);
  111.         
  112.         for (i=0; i < factused-1; i++)
  113.             {
  114.             switch (form[i])
  115.                 {
  116.                 case 0: printf("sin(x*%g)+", fact[i]); break;
  117.                 case 1: printf("cos(x*%g)+", fact[i]); break;
  118.                 }
  119.             }
  120.         switch (form[factused-1])
  121.             {
  122.             case 0: printf("sin(x*%g))\n", fact[factused-1]); break;
  123.             case 1: printf("cos(x*%g))\n", fact[factused-1]); break;
  124.             }
  125.         
  126.         printf("press the mouse buttton to stop ");
  127.         for(i=0; i < factused; i++) printf("*");
  128.         printf("\n");
  129.         
  130.         ShowBear(factused, form, fact);
  131.         
  132.         printf("press any key to continue");
  133.         scanf("%c", response);
  134.         
  135.         } while (true);
  136. exit:    ;
  137.     }
  138.  
  139. ShowBear(int comp, int *type, double *values)
  140.     {
  141.     double    theta = 0, mag = 0;
  142.     Boolean    firsttime = true;
  143.     int        i;
  144.     
  145.     for (theta = 0; theta < 360*onerad; theta += onerad*30)
  146.         for (i=1; i <= comp; i++)
  147.             {
  148.             MoveTo(cos(theta)*(maxradius/comp)*i+width/2,
  149.                 sin(theta)*(maxradius/comp)*i+height/2);
  150.             Line(0,0);
  151.             }
  152.     
  153.     while (!Button())
  154.         {
  155.         mag = 0;
  156.         
  157.         for (i=0; i < comp; i++)
  158.             {
  159.             switch(type[i])
  160.                 {
  161.                 case 0: mag += sin(values[i]*theta); break;
  162.                 case 1: mag += cos(values[i]*theta); break;
  163.                 }
  164.             }
  165.         
  166.         mag *= maxradius/comp;
  167.         
  168.         if (firsttime)
  169.             {
  170.             firsttime = false;
  171.             MoveTo(cos(theta)*mag+width/2, sin(theta)*mag+height/2);
  172.             }
  173.         else
  174.             LineTo(cos(theta)*mag+width/2, sin(theta)*mag+height/2);
  175.         
  176.         theta += onerad*2;
  177.         }
  178.     }
  179.  
  180. Rand(int low, int high)
  181.     {
  182.     int    dist, num;
  183.     
  184.     dist = high-low+1;
  185.     num = Random()%dist;
  186.     num = Abs(num)+low;
  187.     
  188.     return num;
  189.     }
  190.